// Copyright © 2023 ycq_work@163.com. All rights reserved.

/**
 * @Author: ycq_work@163.com
 * @DATE: 2023/10/7
 * @Description: 
*/
#include "SidebarWidget.h"

SidebarWidget::SidebarWidget(QWidget *parent) :
    QWidget(parent)
{
    init();
}

void SidebarWidget::init()
{
    this->setMinimumWidth(200);
    this->setMaximumWidth(300);
    this->setAttribute(Qt::WA_StyledBackground, true);  // 启用样式表对窗口背景的控制

    // 设置样式表，包括背景颜色
    QString styleSheet = "background-color: #A0A083;"
                         "border-radius: 7px;";
    this->setStyleSheet(styleSheet);

    // 创建控件
    auto* onlineMusicLabel = new QLabel;
    onlineMusicLabel->setText(tr("在线音乐"));

    auto* musicHallBtn = new QPushButton;
    setPushButtonIcon(musicHallBtn, QString::fromUtf8(":/icons/musicHall.svg"), tr("音乐馆"));

    auto* recommendedPlaylistBtn = new QPushButton;
    setPushButtonIcon(recommendedPlaylistBtn, QString::fromUtf8(":/icons/recommendedPlaylist.svg"), tr("推荐歌单"));

    auto* musicChartBtn = new QPushButton;
    setPushButtonIcon(musicChartBtn, QString::fromUtf8(":/icons/musicChart.svg"), tr("排行榜"));

    auto* myMusicLabel = new QLabel;
    myMusicLabel->setText(tr("我的音乐"));

    auto* musicPreferencesBtn = new QPushButton;
    setPushButtonIcon(musicPreferencesBtn, QString::fromUtf8(":/icons/musicPreferences.svg"), tr("我喜欢"));

    auto* offlineDownloadBtn = new QPushButton;
    setPushButtonIcon(offlineDownloadBtn, QString::fromUtf8(":/icons/offlineDownload.svg"), tr("本地和下载"));

    auto* recentlyPlayedBtn = new QPushButton;
    setPushButtonIcon(recentlyPlayedBtn, QString::fromUtf8(":/icons/recentlyPlayed.svg"), tr("最近播放"));

    auto* PersonalPlaylistLabel = new QLabel;
    PersonalPlaylistLabel->setText(tr("我的歌单"));

    auto* CreatedPlaylistsBtn = new QPushButton;
    setPushButtonIcon(CreatedPlaylistsBtn, tr("创建的歌单"));

    auto* FavoritedPlaylistsBtn = new QPushButton;
    setPushButtonIcon(FavoritedPlaylistsBtn, tr("收藏的歌单"));

    // 创建按钮组
    buttonGroup.addButton(musicHallBtn, 1);
    buttonGroup.addButton(recommendedPlaylistBtn, 2);
    buttonGroup.addButton(musicChartBtn, 3);
    buttonGroup.addButton(musicPreferencesBtn, 4);
    buttonGroup.addButton(offlineDownloadBtn, 5);
    buttonGroup.addButton(recentlyPlayedBtn, 6);
    buttonGroup.addButton(CreatedPlaylistsBtn, 7);
    buttonGroup.addButton(FavoritedPlaylistsBtn, 8);

    // 使用样式表设置控件的背景透明和高度增加 字体设置
    QFont font("Microsoft YaHei", 10);              // 创建字体，指定字体名称和字体大小
    font.setPointSizeF(font.pointSizeF() * 1.25);         // 将字体大小放大1.25倍

    onlineMusicLabel->setStyleSheet("background-color: transparent; "
                                    "opacity: 0;"
                                    "border-radius: 7px;");
    onlineMusicLabel->setFixedHeight(35);
    onlineMusicLabel->setFont(font);                      // 为标签设置字体

    myMusicLabel->setStyleSheet("background-color: transparent;"
                                "opacity: 0;"
                                "border-radius: 7px;");
    myMusicLabel->setFixedHeight(35);
    myMusicLabel->setFont(font);                          // 为标签设置字体

    PersonalPlaylistLabel->setStyleSheet("background-color: transparent;"
                                         "opacity: 0;"
                                         "border-radius: 7px;");
    PersonalPlaylistLabel->setFixedHeight(35);
    PersonalPlaylistLabel->setFont(font);                 // 为标签设置字体

    font.setPointSizeF(9);
        foreach(QAbstractButton *b, buttonGroup.buttons()) {
            b->setStyleSheet("background-color: transparent;"
                             "opacity: 0;"
                             "border-radius: 7px;");
            b->setFixedHeight(35);
            b->setFont(font);
        }

    // 连接信号与槽，当按钮选中时调用自定义的槽函数
    connect(&buttonGroup, QOverload<int>::of(&QButtonGroup::idClicked), this, &SidebarWidget::onButtonClicked);

    // 设置第一个按钮为默认选中状态
    QAbstractButton *firstButton = buttonGroup.button(1);
    if (firstButton) {
        firstButton->setChecked(true);
        onButtonClicked(1);
    }

    // 创建布局
    auto *layout = new QVBoxLayout;
    layout->addWidget(onlineMusicLabel);
    layout->addWidget(musicHallBtn);
    layout->addWidget(recommendedPlaylistBtn);
    layout->addWidget(musicChartBtn);
    layout->addWidget(myMusicLabel);
    layout->addWidget(musicPreferencesBtn);
    layout->addWidget(offlineDownloadBtn);
    layout->addWidget(recentlyPlayedBtn);
    layout->addWidget(PersonalPlaylistLabel);
    layout->addWidget(CreatedPlaylistsBtn);
    layout->addWidget(FavoritedPlaylistsBtn);

    // 添加一个竖直方向的额弹簧
    auto *spacer = new QSpacerItem(10, 10, QSizePolicy::Minimum, QSizePolicy::Expanding);
    layout->addItem(spacer);
    // 设置布局
    this->setLayout(layout);
}

void SidebarWidget::onButtonClicked(int id)
{
    // 获取选中的按钮
    auto *button = qobject_cast<QPushButton*>(buttonGroup.button(id));

    // 将所有按钮的样式表恢复到初始状态
        foreach(QAbstractButton *b, buttonGroup.buttons()) {
            if (b != button) {
                b->setStyleSheet("background-color: transparent;"
                                 "opacity: 0;"
                                 "border-radius: 7px;");
            }
        }

    // 更改选中按钮的样式
    if (button) {
        button->setStyleSheet("background-color: rgba(0, 0, 0, 0.2);"
                              "border-radius: 7px;");

        emit buttonClicked(id);
    }
}

void SidebarWidget::setPushButtonIcon(QPushButton *button, const QString& url, const QString& text)
{
    button->setText(setPushButtonTextSize(text + static_cast<QString>("    ")));
    QIcon icon;
    icon.addFile(url, QSize(), QIcon::Normal, QIcon::Off);
    button->setIcon(icon);
}

void SidebarWidget::setPushButtonIcon(QPushButton *button, const QString& text)
{
    button->setText(setPushButtonTextSize(text + "                "));
}

QString SidebarWidget::setPushButtonTextSize(QString str)
{
    uint stringSize = QString("觅心阁音乐呀").size();
    if (str.size() == stringSize) {
        return str;
    }
    while(str.size() < stringSize) {
        str += " "; // 添加一个空格
    }
    while(str.size() > stringSize) {
        str.remove(QRegExp("\\s")); // 删除空格
    }
    return str;
}





